﻿

namespace Mulala.Cavan.Application.MainBoundedContext.ChargeModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Domain.MainBoundedContext.ChargeModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Domain.Seedwork;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;
    using Spring.Context;
    using Mulala.Cavan.Application.MainBoundedContext.CorporationModule;
    using Spring.Context.Support;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Application.MainBoundedContext.SecurityModule;

    public class UserServiceAppService : BaseAppService<UserService, UserServiceSearch, IUserServiceRepository>, IUserServiceAppService
    {
        private IApplicationContext SpringContext = null;
        private ICorporationAppService CorporationAppService { get; set; }
        private IUserAccountAppService UserAccountAppService { get; set; }

        public BoolGenericResult<Corporation> AddUserServiceToCorporation(int corporationID, int userServiceID)
        {
            this.SpringContext = ContextRegistry.GetContext();
            this.CorporationAppService = this.SpringContext.GetObject(MulalaAppServiceConst.CORPORATIONAPPSERVICE) as ICorporationAppService;
            this.UserAccountAppService = this.SpringContext.GetObject(MulalaAppServiceConst.USERACCOUNTAPPSERVICE) as IUserAccountAppService;

            Corporation corporation = this.CorporationAppService.GetById(corporationID);
            UserService userService = this.Repository.Get(userServiceID);

            BoolGenericResult<Corporation> result = corporation.SubscribeUserService(userService);

            if (result.IsSuccess)
            {
                this.CorporationAppService.UpdateCorporationStatus(corporation.CorporationStatus);
                this.UserAccountAppService.AddOrUpdate(corporation.UserAccount);
            }

            return result;
        }
    }
}
